/********************************************************************************
 * Copyright 2001 - 2015 Comit. All Rights Reserved.
 * 作者：RockeyCai
 * 日期：2015-8-28 15:58:36
 * 描述：瑜伽资源表的映射模型。(自动生成，代码模块1.0v)
 * *****************************************************************************/
var cheerio = require('cheerio');
var superagent = require('superagent');
module.exports = function(BASKETBALL) {

  /**
   * 添加瑜伽资源表对象
   * @param req
   * @param res
   */
  BASKETBALL.addModel = function (req, res , index) {

    console.log('addModel');

    superagent.get('http://v.youku.com/v_vpvideoplaylistv5?__rt=1&__ro=&s=0&f=21723166&o=1&vid=165759772&pl=50&pn=' + index +  '&t=2')
      .end(function (err, sres) {
        if (err) {
          console.log(err)
        }

        var $ = cheerio.load(sres.text);

        var liList = $('li');
        //获取标题
        liList.each(function (idx, element) {
          //item_
          var id = $(element).attr('id');
          id = id.substring( 5 , id.length);
          var title = $(element).attr('title');
          var l_order = $(element).find('.l_order .num');
          var l_time = $(element).find('.l_time .num');
          var img = $(element).find('img').attr('_src');

          var item = {};
          item.URL = 'http://player.youku.com/embed/' + id;
          item.ID = l_order.text();
          item.IMG = img;
          item.TIME = l_time.text();
          item.TITLE = title;

          //保存记录
          BASKETBALL.create(item , function(err, obj){
            if (err) {
            }
          });
        });
      });

  }
}
